# -*- coding: utf-8 -*-
"""
现代化深色主题样式定义
实现用户要求的现代化深色主题UI
"""

from PyQt6.QtWidgets import QApplication
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QPalette, QColor

# 主题颜色定义
COLORS = {
    # 主要背景色
    'background': '#282c34',           # 深灰色背景
    'card_background': '#3c4049',      # 卡片/控件背景色
    'input_background': '#4a4e57',     # 输入框背景色
    
    # 强调色
    'primary': '#00a1d6',              # B站标志性蓝色
    'primary_hover': '#0091c2',        # 主色悬停
    'primary_pressed': '#007ba3',      # 主色按下
    
    # 文本颜色
    'text_primary': '#ffffff',         # 主要文本（白色）
    'text_secondary': '#b0b3b8',       # 次要文本（浅灰色）
    'text_disabled': '#6e7681',        # 禁用文本
    'text_placeholder': '#8b949e',     # 占位符文本
    
    # 边框颜色
    'border': '#444c56',               # 默认边框
    'border_focus': '#00a1d6',         # 焦点边框
    'border_hover': '#656d76',         # 悬停边框
    
    # 状态颜色
    'success': '#238636',              # 成功（绿色）
    'warning': '#d29922',              # 警告（橙色）
    'error': '#da3633',                # 错误（红色）
    'info': '#0969da',                 # 信息（蓝色）
    
    # 按钮颜色
    'button_secondary': '#21262d',     # 次要按钮背景
    'button_secondary_hover': '#30363d', # 次要按钮悬停
    'button_danger': '#da3633',        # 危险按钮
    'button_danger_hover': '#c93c37',  # 危险按钮悬停
    
    # 滚动条颜色
    'scrollbar_background': '#2d333b', # 滚动条背景
    'scrollbar_handle': '#545d68',     # 滚动条手柄
    'scrollbar_handle_hover': '#636e7b', # 滚动条手柄悬停
}

def get_dark_theme_stylesheet() -> str:
    """
    获取深色主题样式表
    
    Returns:
        str: 完整的QSS样式表
    """
    return f"""
    /* 全局样式 */
    QWidget {{
        background-color: {COLORS['background']};
        color: {COLORS['text_primary']};
        font-family: 'Microsoft YaHei', 'Segoe UI', sans-serif;
        selection-background-color: {COLORS['primary']};
        selection-color: {COLORS['text_primary']};
    }}
    
    /* 主窗口样式 */
    QMainWindow {{
        background-color: {COLORS['background']};
        border: none;
    }}
    
    /* 输入框样式 */
    QLineEdit {{
        background-color: {COLORS['input_background']};
        border: 2px solid {COLORS['border']};
        border-radius: 8px;
        padding: 8px 12px;
        font-size: 14px;
        color: {COLORS['text_primary']};
        min-height: 20px;
    }}
    
    QLineEdit:focus {{
        border-color: {COLORS['border_focus']};
        background-color: {COLORS['card_background']};
    }}
    
    QLineEdit:hover {{
        border-color: {COLORS['border_hover']};
    }}
    
    QLineEdit::placeholder {{
        color: {COLORS['text_placeholder']};
    }}
    
    /* 按钮样式 */
    QPushButton {{
        background-color: {COLORS['button_secondary']};
        border: 2px solid {COLORS['border']};
        border-radius: 8px;
        padding: 10px 20px;
        font-size: 14px;
        font-weight: 500;
        color: {COLORS['text_primary']};
        min-height: 16px;
    }}
    
    QPushButton:hover {{
        background-color: {COLORS['button_secondary_hover']};
        border-color: {COLORS['border_hover']};
    }}
    
    QPushButton:pressed {{
        background-color: {COLORS['border']};
    }}
    
    QPushButton:disabled {{
        background-color: {COLORS['background']};
        color: {COLORS['text_disabled']};
        border-color: {COLORS['border']};
    }}
    
    /* 主要按钮样式 */
    QPushButton[buttonType="primary"] {{
        background-color: {COLORS['primary']};
        border-color: {COLORS['primary']};
        color: {COLORS['text_primary']};
        font-weight: 600;
    }}
    
    QPushButton[buttonType="primary"]:hover {{
        background-color: {COLORS['primary_hover']};
        border-color: {COLORS['primary_hover']};
    }}
    
    QPushButton[buttonType="primary"]:pressed {{
        background-color: {COLORS['primary_pressed']};
        border-color: {COLORS['primary_pressed']};
    }}
    
    /* 危险按钮样式 */
    QPushButton[buttonType="danger"] {{
        background-color: {COLORS['button_danger']};
        border-color: {COLORS['button_danger']};
        color: {COLORS['text_primary']};
    }}
    
    QPushButton[buttonType="danger"]:hover {{
        background-color: {COLORS['button_danger_hover']};
        border-color: {COLORS['button_danger_hover']};
    }}
    
    /* 文本区域样式 */
    QTextEdit, QPlainTextEdit {{
        background-color: {COLORS['card_background']};
        border: 2px solid {COLORS['border']};
        border-radius: 8px;
        padding: 12px;
        font-size: 13px;
        color: {COLORS['text_primary']};
        font-family: 'Consolas', 'Monaco', monospace;
    }}
    
    QTextEdit:focus, QPlainTextEdit:focus {{
        border-color: {COLORS['border_focus']};
    }}
    
    /* 标签样式 */
    QLabel {{
        color: {COLORS['text_primary']};
        background-color: transparent;
        font-size: 14px;
    }}
    
    QLabel[labelType="title"] {{
        font-size: 18px;
        font-weight: 600;
        color: {COLORS['text_primary']};
    }}
    
    QLabel[labelType="subtitle"] {{
        font-size: 16px;
        font-weight: 500;
        color: {COLORS['text_secondary']};
    }}
    
    QLabel[labelType="caption"] {{
        font-size: 12px;
        color: {COLORS['text_secondary']};
    }}
    
    /* 下拉框样式 */
    QComboBox {{
        background-color: {COLORS['input_background']};
        border: 2px solid {COLORS['border']};
        border-radius: 8px;
        padding: 8px 12px;
        font-size: 14px;
        color: {COLORS['text_primary']};
        min-height: 20px;
    }}
    
    QComboBox:hover {{
        border-color: {COLORS['border_hover']};
    }}
    
    QComboBox:focus {{
        border-color: {COLORS['border_focus']};
    }}
    
    QComboBox::drop-down {{
        border: none;
        width: 20px;
    }}
    
    QComboBox::down-arrow {{
        image: none;
        border-left: 5px solid transparent;
        border-right: 5px solid transparent;
        border-top: 5px solid {COLORS['text_secondary']};
        margin-right: 5px;
    }}
    
    QComboBox QAbstractItemView {{
        background-color: {COLORS['card_background']};
        border: 2px solid {COLORS['border']};
        border-radius: 8px;
        color: {COLORS['text_primary']};
        selection-background-color: {COLORS['primary']};
        outline: none;
    }}
    
    /* 滚动条样式 */
    QScrollBar:vertical {{
        background-color: {COLORS['scrollbar_background']};
        width: 12px;
        border-radius: 6px;
        margin: 0;
    }}
    
    QScrollBar::handle:vertical {{
        background-color: {COLORS['scrollbar_handle']};
        border-radius: 6px;
        min-height: 20px;
        margin: 2px;
    }}
    
    QScrollBar::handle:vertical:hover {{
        background-color: {COLORS['scrollbar_handle_hover']};
    }}
    
    QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {{
        height: 0;
        background: none;
    }}
    
    QScrollBar:horizontal {{
        background-color: {COLORS['scrollbar_background']};
        height: 12px;
        border-radius: 6px;
        margin: 0;
    }}
    
    QScrollBar::handle:horizontal {{
        background-color: {COLORS['scrollbar_handle']};
        border-radius: 6px;
        min-width: 20px;
        margin: 2px;
    }}
    
    QScrollBar::handle:horizontal:hover {{
        background-color: {COLORS['scrollbar_handle_hover']};
    }}
    
    QScrollBar::add-line:horizontal, QScrollBar::sub-line:horizontal {{
        width: 0;
        background: none;
    }}
    
    /* 分组框样式 */
    QGroupBox {{
        background-color: {COLORS['card_background']};
        border: 2px solid {COLORS['border']};
        border-radius: 12px;
        margin-top: 10px;
        padding-top: 10px;
        font-size: 14px;
        font-weight: 500;
        color: {COLORS['text_primary']};
    }}
    
    QGroupBox::title {{
        subcontrol-origin: margin;
        left: 15px;
        padding: 0 8px 0 8px;
        background-color: {COLORS['card_background']};
        color: {COLORS['text_primary']};
    }}
    
    /* 进度条样式 */
    QProgressBar {{
        background-color: {COLORS['card_background']};
        border: 2px solid {COLORS['border']};
        border-radius: 8px;
        text-align: center;
        font-size: 12px;
        color: {COLORS['text_primary']};
        height: 20px;
    }}
    
    QProgressBar::chunk {{
        background-color: {COLORS['primary']};
        border-radius: 6px;
        margin: 2px;
    }}
    
    /* 状态栏样式 */
    QStatusBar {{
        background-color: {COLORS['card_background']};
        border-top: 1px solid {COLORS['border']};
        color: {COLORS['text_secondary']};
        font-size: 12px;
    }}
    
    /* 菜单样式 */
    QMenuBar {{
        background-color: {COLORS['card_background']};
        border-bottom: 1px solid {COLORS['border']};
        color: {COLORS['text_primary']};
    }}
    
    QMenuBar::item {{
        background-color: transparent;
        padding: 8px 12px;
    }}
    
    QMenuBar::item:selected {{
        background-color: {COLORS['primary']};
    }}
    
    QMenu {{
        background-color: {COLORS['card_background']};
        border: 2px solid {COLORS['border']};
        border-radius: 8px;
        color: {COLORS['text_primary']};
    }}
    
    QMenu::item {{
        padding: 8px 20px;
    }}
    
    QMenu::item:selected {{
        background-color: {COLORS['primary']};
    }}
    
    /* 工具提示样式 */
    QToolTip {{
        background-color: {COLORS['card_background']};
        border: 2px solid {COLORS['border']};
        border-radius: 6px;
        padding: 6px;
        color: {COLORS['text_primary']};
        font-size: 12px;
    }}
    
    /* 分隔线样式 */
    QFrame[frameShape="4"] {{ /* HLine */
        background-color: {COLORS['border']};
        max-height: 1px;
        border: none;
    }}
    
    QFrame[frameShape="5"] {{ /* VLine */
        background-color: {COLORS['border']};
        max-width: 1px;
        border: none;
    }}
    """

def apply_dark_theme(app: QApplication):
    """
    应用深色主题到应用程序
    
    Args:
        app: QApplication实例
    """
    # 设置样式表
    app.setStyleSheet(get_dark_theme_stylesheet())
    
    # 设置调色板（作为备用）
    palette = QPalette()
    
    # 窗口颜色
    palette.setColor(QPalette.ColorRole.Window, QColor(COLORS['background']))
    palette.setColor(QPalette.ColorRole.WindowText, QColor(COLORS['text_primary']))
    
    # 基础颜色
    palette.setColor(QPalette.ColorRole.Base, QColor(COLORS['card_background']))
    palette.setColor(QPalette.ColorRole.AlternateBase, QColor(COLORS['input_background']))
    
    # 文本颜色
    palette.setColor(QPalette.ColorRole.Text, QColor(COLORS['text_primary']))
    palette.setColor(QPalette.ColorRole.PlaceholderText, QColor(COLORS['text_placeholder']))
    
    # 按钮颜色
    palette.setColor(QPalette.ColorRole.Button, QColor(COLORS['button_secondary']))
    palette.setColor(QPalette.ColorRole.ButtonText, QColor(COLORS['text_primary']))
    
    # 高亮颜色
    palette.setColor(QPalette.ColorRole.Highlight, QColor(COLORS['primary']))
    palette.setColor(QPalette.ColorRole.HighlightedText, QColor(COLORS['text_primary']))
    
    # 应用调色板
    app.setPalette(palette)
    
    print("深色主题应用成功")

def get_color(color_name: str) -> str:
    """
    获取主题颜色
    
    Args:
        color_name: 颜色名称
        
    Returns:
        str: 颜色值
    """
    return COLORS.get(color_name, '#ffffff')